package com.yoyo.common.core.service.impl;

import com.yoyo.common.core.enums.ErrorCode;
import com.yoyo.common.core.service.ErrorMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Service;

import java.util.Locale;

@Service
public class ErrorMessageServiceImpl implements ErrorMessageService {
    @Autowired
    private MessageSource messageSource;
    @Override
    public String getErrorMessage(ErrorCode errorCode, Locale locale) {
        try {

            return messageSource.getMessage("error." + errorCode.getCode(), null, locale);
        } catch (NoSuchMessageException e) {
            return "Unknown error";
        }
    }
}
